{extend name="base" /}
<!-- page specific plugin styles -->
{block name="plugin-styles"}{/block}
<!-- inline styles related to this page -->
{block name="inline-styles"}
<link rel="stylesheet" href="/static/ace/assets/css/colorbox.min.css" />
<style>
th,td{
    text-align:  center !important;
}
</style>
{/block}
<!-- breadcrumb -->
{block name="breadcrumbs"}
<ul class="breadcrumb">
    <li>
        <i class="ace-icon fa fa-home home-icon"></i>
        <a href="{:url('Index/index')}">首页</a>
    </li>
    <li class="active">地址管理</li>
</ul>
<!-- /.breadcrumb -->
{/block}
<!-- PAGE CONTENT BEGINS -->
{block name="content"}
<div class="page-header">
    <h1>
		地址列表
	</h1>
</div>
<!-- /.page-header -->
<div class="clearfix tableTools-container">
    <form class="pull-left form-inline">
        <input type="hidden" class="form-control input-sm" name="mid" value='{$mid}' >
        <div class="form-group">
            <label class="sr-only" for="title">代理关键字</label>
            <div class="input-group">
                <span class="input-group-addon">代理关键字</span>
                <input type="text" class="form-control input-sm" name="keywords" placeholder="代理姓名或手机号">
            </div>
        </div>
        <div class="form-group">
            <label class="sr-only" for="title">姓名</label>
            <div class="input-group">
                <span class="input-group-addon">姓名</span>
                <input type="text" class="form-control input-sm" name="name" placeholder="姓名">
            </div>
        </div>
        <div class="form-group">
            <label class="sr-only" for="title">手机</label>
            <div class="input-group">
                <span class="input-group-addon">手机</span>
                <input type="text" class="form-control input-sm" name="tel" placeholder="手机">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label" for="title">类型</label>
            <select class="form-control input-sm" name="type" id="">
                <option value="">全部</option>
                <option value="1" {eq name='type' value='1'}selected{/eq}>收货人</option>
                <option value="2" {eq name='type' value='2'}selected{/eq}>发货人</option>
            </select>
        </div>
        <div class="form-group">
        	<button type="button" class="btn btn-xs btn-primary" id="searchTable">搜索</button>
        	<button type="reset" class="btn btn-xs btn-primary" >重置</button>
        </div>
    </form>
</div>
<div class="table-responsive">
	<table class="table table-bordered" id="DynamicTable"></table>
</div>
<!-- /.table-responsive -->
{/block}
<!-- PAGE CONTENT ENDS -->
<!-- page specific plugin scripts -->
{block name="plugin-scripts"}
<script src="/static/ace/assets/js/jquery.dataTables.min.js"></script>
<script src="/static/ace/assets/js/jquery.dataTables.bootstrap.min.js"></script>
<script src="/static/ace/assets/js/jquery.colorbox.min.js"></script>
{/block}
<!-- inline scripts related to this page -->
{block name="related-scripts"}
<script type="text/javascript">
	var myTable = 
	$('#DynamicTable')
	.wrap("<div class='dataTables_borderWrap' />")   //if you are applying horizontal scrolling (sScrollX)
	.DataTable({
		"autoWidth": false,	//智能列宽处理
		"deferRender": true,	//延迟渲染以获得额外的初始化速度
		"info": true,	//表信息显示
		"lengthChange": false,	//更改每页记录数
		"ordering": false,	//排序功能
		"paging": true,	//启用或禁用表分页
		"processing": false,	//处理指标
		// "scrollX": false,	//水平滚动
		// "scrollY": false,	//垂直滚动
		"searching": false,	//搜索功能
		"serverSide": true,	//服务器端处理模式
		"stateSave": false,	//状态保存(页面重新加载时恢复表状态)
		"language": {
            "url": "//cdn.datatables.net/plug-ins/1.10.19/i18n/Chinese.json"
        },
		"ajax": {
            "url": "/admin/agent/address_rs",
            "type": "POST",
            "data": function (d) {
                d.keywords = $("input[name=keywords]").val();
                d.name = $("input[name=name]").val();
                d.tel = $("input[name=tel]").val();
                d.type = $("select[name=type]").val();
                d.mid=$("input[name=mid]").val();
            },
            "dataSrc": function (json) {
                return json.data;
            }
        },
        "columns": [{
            "title": "编号",
            "data": "id"
        }, {
            "title": "代理姓名",
            "data": "agent_name"
        },{
            "title": "类型",
            "data": "type"
        },{
            "title": "姓名",
            "data": "name"
        }, {
            "title": "电话",
            "data": "tel"
        }, {
            "title": "地址",
            "data": "address"
        },{
            "title": "添加时间",
            "data": "addtime"
        }, {
            "title": "是否默认",
            "data": "ifdefault"
        }, {
            "title": "操作",
            "data": "id"
        }, ],
        "columnDefs": [{
            "render": function (data, type, row, meta) {
            	return '<a href="{:url('agent/detail')}?mid='+row.mid+'" title="'+data+'" target="_blank">'+data+'</a>'
            },
            "targets": 1
        },{
            "render": function (data, type, row, meta) {
                if(data==1){
                    return '<span class="label label-success arrowed-in">收货人</span>'
                }else if(data == 2){
                    return '<span class="label label-inverse arrowed-right">发货人</span>'
                }
            },
            "targets": 2
        }, {
            "render": function (data, type, row, meta) {
                if(data==1){
                    return '<span class="badge badge-success">是</span>'
                }else{
                    return '<span class="badge badge-grey">否</span>'
                }
            },
            "targets": 7
        }, {
        	"render": function (data, type, row, meta) {
                return '<div class="btn-group">\
                             <a class="btn btn-xs btn-success" target="_blank" title = "修改" href="{:url('agent/address_edit')}?id='+row.id+'"">\
                                <i class="ace-icon glyphicon glyphicon-pencil bigger-120"></i>\
                             </a>\
	                         <a class="btn btn-xs btn-pink" target="_blank" title ="添加" href="{:url('agent/address_add')}?mid='+row.mid+'">\
	                            <i class="ace-icon glyphicon glyphicon-plus bigger-120"></i>\
	                         </a>&nbsp;&nbsp;\
                        </div>';
            },
            "targets": 8
        }],
        "initComplete": function( settings, json ) {
        	$("#DynamicTable").prev(".row").remove();
        	//add tooltip for small view
			$('[data-rel="tooltip"]').tooltip()
            $('[data-rel="colorbox"]').colorbox(colorbox_params);;
		}
	});

    $("#searchTable").click(function () {
        myTable.draw();
    });
    
    var $overflow = '';
    var colorbox_params = {
        rel: 'colorbox',
        reposition:true,
        scalePhotos:true,
        scrolling:false,
        previous:'<i class="ace-icon fa fa-arrow-left"></i>',
        next:'<i class="ace-icon fa fa-arrow-right"></i>',
        close:'&times;',
        current:'{current} of {total}',
        maxWidth:'100%',
        maxHeight:'100%',
        onOpen:function(){
            $overflow = document.body.style.overflow;
            document.body.style.overflow = 'hidden';
        },
        onClosed:function(){
            document.body.style.overflow = $overflow;
        },
        onComplete:function(){
            $.colorbox.resize();
        }
    };

    
    $("#cboxLoadingGraphic").html("<i class='ace-icon fa fa-spinner orange fa-spin'></i>");//let's add a custom loading icon
    
    
    $(document).one('ajaxloadstart.page', function(e) {
        $('#colorbox, #cboxOverlay').remove();
    });
    
    // 查看
    function showModal(_id){
        $.ajax({
            url: '/admin/agent/find',
            type: 'GET',
            dataType: 'json',
            data: {id: _id},
        })
        .done(function(json) {
            console.log("success");
            initPageHTML('myModal',json.data);
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
    }
    // 显示充值框
    function showModalRecharge(_id){
    	$("#remark").val('');
    	$('#rechargeBtn').attr("disabled", false);
        $("#myModalRecharge").data('id',_id);
    }
    
    //第一次充值 
    function showFirstModalRecharge(_id){
    	$('#firstRechargeBtn').attr("disabled", false);
    	$("#yajin").val('');
    	$("#remark").val('');
    	 $("#myFirstModalRecharge").data('id',_id);
    }
    // 充值
    function recharge() {
    	$('#rechargeBtn').attr("disabled", true);
        // body...
        $.ajax({
            url: "/admin/recharge/add",
            type: 'POST',
            dataType: 'json',
            data: {
                mid: $("#myModalRecharge").data("id"),
                money: $("#money").val(),
                remark: $("#remark").val()
            },
        })
        .done(function(json) {
            console.log("success");
            if(json.returnCode==1){
            	
                alert("充值成功");
                $("#myModalRecharge").modal('hide');
                myTable.draw();
                
            }else{
                alert(json.returnMsg);
            }
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
    }
    
    function firstRecharge(){
    	$('#firstRechargeBtn').attr("disabled", true);
    	
    	 // body...
        $.ajax({
            url: "/admin/recharge/first_add",
            type: 'POST',
            dataType: 'json',
            data: {
                mid: $("#myFirstModalRecharge").data("id"),
                money: $("#first_money").val(),
                remark: $("#first_remark").val(),
                yajin: $("#yajin").val()
            },
        })
        .done(function(json) {
            console.log("success");
            if(json.returnCode==1){
            	
                alert("充值成功");
                $("#myFirstModalRecharge").modal('hide');
                myTable.draw();
                
                
            }else{
                alert(json.returnMsg);
            }
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
    }
    // 删除
    function isDelete(_id){
        var _istTrue = confirm("确认删除该数据？");
        if (r==true) {
            console.log("You pressed OK!")
        }else {
            console.log("You pressed Cancel!")
        }
    }
    
    function reset(_id){
    	if(!confirm("确认要重置密码？")){
    		return;
    	}
    	$.ajax({
            url: "/admin/agent/reset_pwd",
            type: 'POST',
            dataType: 'json',
            data: {
                mid:_id,
            },
        })
        .done(function(json) {
            console.log("success");
            if(json.returnCode==1){
                alert("重置成功");
            }else{
                alert(json.returnMsg);
            }
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
    }

    // 显示更换代理人
    function showAgentModal(_id){
        $("#myModalAgent").data('id',_id);
    }

    function update_agent(){
        $.ajax({
            url: "/admin/agent/update_agent",
            type: 'POST',
            dataType: 'json',
            data: {
                mid: $("#myModalAgent").data("id"),
                pid: $("#pid").val()
            },
        })
        .done(function(json) {
            console.log("success");
            if(json.returnCode==1){
                alert("操作成功");
                myTable.draw();
                $("#myModalAgent").modal('hide');
            }else{
                alert(json.returnMsg);
            }
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
    }
    
</script>
{/block}